package com.ww.springboot.boot.javabase.hashmap;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2022-01-17 10:27
 */
public class ConcurrentDemo {
    static final int HASH_BITS = 0x7fffffff;

    public static void main(String[] args) {
        //1.线程不安全
        Map<String, String> map = new HashMap<>();
        //2.线程安全 --锁过多 每个操作都加synchronized
        Map<String, String> map2 = new Hashtable<>();
        //3.线程安全 分段锁 -根据hash值分段 并对对应段进行加锁
        Map<String, String> map3 = new ConcurrentHashMap<>();
        map3.put("a", "1");
        map3.put("a", "1");
        concurrentHashMap();

        //4.有序的 按照KEY值根据 Comparator 进行排序 非线程安全
        Map<String, String> map4 = new TreeMap<>();
        //5.有序的 按照插入顺序排序 非线程安全
        Map<String, String> map5 = new LinkedHashMap<>();
    }

    private static void concurrentHashMap() {
        for (int i = 0; i < 100; i++) {
            System.out.println(HASH_BITS);
            System.out.println(i & HASH_BITS);
            //此方法是让hash值高位与低位都参与到散列;避免一些特殊数据散列不均匀
            System.out.println(("x".hashCode() ^ ("x".hashCode() >>> 16)) & HASH_BITS);
        }

    }

}
